import { Injectable } from '@nestjs/common';
import { QueryReportRecordDto } from './dto/query-report-record.dto';
import { PrismaService } from 'nestjs-prisma';
import * as utils from 'xe-utils';

@Injectable()
export class ReportService {
  constructor(readonly prisma: PrismaService) {}

  async getRecord(query: QueryReportRecordDto) {
    const users = await this.prisma.worker.findMany({
      where: {
        disabled: false,
        name: {
          contains: query.worker || undefined,
        },
      },
      select: {
        id: true,
        name: true,
        record: {
          where: {
            date: {
              lte: new Date(query.endDate + ' 23:59:59'),
              gt: new Date(query.startDate + ' 00:00:00'),
            },
            style: {
              groupId: query.group || undefined,
            },
          },
          include: {
            size: {
              select: {
                id: true,
                name: true,
              },
            },
            style: {
              include: {
                process: {
                  select: {
                    id: true,
                    name: true,
                    index: true,
                    prices: true,
                  },
                },
                group: {
                  select: {
                    id: true,
                    name: true,
                  },
                },
                model: {
                  select: {
                    id: true,
                    code: true,
                    name: true,
                  },
                },
                sizes: true,
              },
            },
          },
        },
      },
    });

    const list = users.filter((user) => user.record.length > 0);

    const record: any = [];
    for (const item of list) {
      for (const row of item.record) {
        const { style, size, sizeId, number } = row;
        const process = await this.getProcess(style.process, row.processIndex);

        if (!process) {
          console.log(row);
          return;
        }

        const price = process.prices.find((p) => p.sizeId == sizeId);
        const obj = {
          worker: item.name,
          styleName: `${style.group.name} ${style.model.code} (${style.model.name}) ${size.name}`,
          process: `${process.index}-${process.name}`,
          number: number,
          price: price.price,
          amount: number * price.price,
        };
        record.push(obj);
      }
    }

    return record;

    // list.forEach((item: any) => {
    //   item.record = this.getGroup(item.record) as any;
    //
    //   let amount = 0;
    //   item.record.forEach((r: any) => {
    //     amount += r.amount;
    //   });
    //   item.amount = amount;
    // });
    //
    // return list;
  }

  getProcess(process: any[], index: number) {
    return process.find((p) => p.index == index);
  }

  getGroup(record: any) {
    const ids: string[] = utils.uniq(
      record.map((r: any) => r.processId + '&' + r.sizeId),
    );

    console.log(ids);

    return ids.map((id) => {
      const [processId, sizeId] = id.split('&');

      const filterRecord = record.filter(
        (r: any) => r.processId == processId && r.sizeId == sizeId,
      );

      const { style, size, process } = filterRecord[0];

      const price = process.prices.find((p: any) => p.sizeId == sizeId);

      const obj = {
        number: 0,
        price: price.price,
        amount: 0,
        process: process,
        styleName: `${style.group.name} ${style.model.code} (${style.model.name}) ${size.name}`,
      };

      filterRecord.forEach((row: any) => {
        obj.number += row.number;
      });
      obj.amount = obj.number * obj.price;

      return obj;
    });
  }
}
